#include "stdafx.h"
#include "Clock.h"
#include "Render.h"

CClock::CClock(void)
{
	m_pImgClock = NULL;
	m_pImgNumber = NULL;
	m_pImgArrow = NULL;

	m_nDirection = Direction_Left;
}

CClock::~CClock(void)
{
}

void CClock::StartClock(int nSecond)
{
    m_timer.StartTimer(nSecond);
	PlayFrame(nSecond, 1000);
	SetVisible(TRUE);
}

BOOL CClock::Timeout()
{
    return m_timer.CountingTimer();
}

void CClock::StopClock()
{
    m_timer.Pause(TRUE);
	EndPlay();
	SetVisible(FALSE);
}

void CClock::SetImage(CxImage *pImgClock, CxImage *pImgNumber, CxImage *pImgArrow)
{
	m_pImgClock = pImgClock;
	m_pImgNumber = pImgNumber;
	m_pImgArrow = pImgArrow;
}

void CClock::Render(CDC *pDC)
{
	int nArrowWidth = 0;
	int nArrowHeight = 0;
	int nClockWidth = 0;
	int nClockHeight = 0;

	if(m_pImgArrow != NULL && m_pImgArrow->IsValid())
	{
	    nArrowWidth = m_pImgArrow->GetWidth() / 4;
	    nArrowHeight = m_pImgArrow->GetHeight();
	}

	if(m_pImgClock != NULL && m_pImgClock->IsValid())
	{
		nClockWidth = m_pImgClock->GetWidth();
		nClockHeight = m_pImgClock->GetHeight();
        m_pImgClock->Draw(pDC->m_hDC, m_nCurPosX + (m_nWidth - nClockWidth) / 2, m_nCurPosY + (m_nHeight - nClockHeight) / 2);
	}

	if(m_pImgArrow != NULL && m_pImgArrow->IsValid())
	{
		int nSrcOffset = 0;
		CRect rcArrow;
		switch(m_nDirection)
		{
		case Direction_Left:
			rcArrow = CRect(m_nCurPosX, m_nCurPosY + (m_nHeight - nArrowHeight) / 2, m_nCurPosX + nArrowWidth, m_nCurPosY + (m_nHeight + nArrowHeight) / 2);
			nSrcOffset = 3;
			break;
		case Direction_Top:
			rcArrow = CRect(m_nCurPosX + (m_nWidth - nArrowWidth) / 2, m_nCurPosY, m_nCurPosX + (m_nWidth + nArrowWidth) / 2, m_nCurPosY + nArrowHeight);
			nSrcOffset = 0;
			break;
		case Direction_Right:
			rcArrow = CRect(m_nCurPosX + nArrowWidth + nClockWidth, m_nCurPosY + (m_nHeight - nArrowHeight) / 2, m_nCurPosX + m_nWidth, m_nCurPosY + (m_nHeight + nArrowHeight) / 2);
			nSrcOffset = 1;
			break;
		case Direction_Bottm:
			rcArrow = CRect(m_nCurPosX + (m_nWidth - nArrowWidth) / 2, m_nCurPosY + m_nHeight - nArrowHeight, m_nCurPosX + (m_nWidth + nArrowWidth) / 2, m_nCurPosY + m_nHeight);
			nSrcOffset = 2;
			break;
		}

		CRect rcSrc(nSrcOffset * nArrowWidth, 0, nSrcOffset * nArrowWidth + nArrowWidth, nArrowHeight);
		m_pImgArrow->DrawPart(pDC->m_hDC, rcArrow, rcSrc);
	}

	if(m_pImgNumber != NULL && m_pImgNumber->IsValid())
	{
		int nTotalSecond = m_timer.GetRestTime() / 1000;
		CRect rcClock(m_nCurPosX + (m_nWidth - nClockWidth) / 2, m_nCurPosY + (m_nHeight - nClockHeight) / 2, m_nCurPosX + (m_nWidth + nClockWidth) / 2, m_nCurPosY + (m_nHeight + nClockHeight) / 2);
		CRender::DrawPicNum(pDC, &rcClock, nTotalSecond, *m_pImgNumber, DT_CENTER | DT_VCENTER, 10);
	}
}

